
import antlr.collections.AST;



import java.util.*;


public class TwinkleFunction
{
	AST body;
	String name;
	static Hashtable functions =new Hashtable(); 
	
	public TwinkleFunction(String name,  AST body)
	{
	 this.name= name;
	 this.body = body;
	}
	
	public static void funcRegister(String name,  AST body ) throws
	antlr.RecognitionException
	
	{
		if(functions.get(name)!= null)
		{
			System.out.println("Function "+ name+ " is aleady defined. ");	
			System.exit(1);
		}

		TwinkleFunction function = new TwinkleFunction(name, body);
		functions.put(name, function);	  
	}
	
	public static void funcCall( TwinkleTreeParser walker, String name, Env env)
	{
		try
		{
			TwinkleFunction function = (TwinkleFunction)functions.get(name);
			
			if(function == null)
			{
				System.out.println("Function "+name+" is not defined.");
				System.exit(1);
			}
			walker.subprogram( function.getBody() ) ;
		}
		
		catch(Exception e)
		{
		
		}
	}
	
	public AST getBody()
	{
		return body;
	}
}
